/*-----------------
 图片幻灯片效果
 ------------------*/
define(function(require,exports,module){
    var $ = require("$");
    //定义类
    function Magic(obj){
        this.obj = $(obj);//滚动图片容器
        this.$imgList = this.obj.find("ul:first");//图片列表
        this.$imgs = this.$imgList.find("li");//图片
        this._max =  this.$imgList.find("li").length;//图片个数
        this._width = this.$imgList.find("li:first").width();//图片宽度
        this._current = 1;//当前索引
        this.$next = this.obj.find(".next");//下一个 按钮
        this.$pre = this.obj.find(".pre");//上一个 按钮

        //获取图片导航
        var $vector = this.obj.find(".index-vector span");//导航
        var $indexNum = this.obj.find(".img-scroll-num li");
        var $nav;
        if($vector.length>0){ $nav = $vector; }else{ $nav = $indexNum; }
        this.$nav = $nav;

        //css
        this.NEXT_CSS = "magic-img-next";
        this.PRE_CSS = "magic-img-pre";
        this.NOW_CSS = "magic-img-now";
    }

    module.exports = Magic;//对外提供的接口
    //下一个
    Magic.prototype.next = function(){
        var that = this;
        var next;
        if (this._current === this._max) {
            next = 1;
        }
        else {
            next = that._current + 1;
        }
        return next;
    }
    //上一个
    Magic.prototype.pre = function(){
        var that = this;
        var pre;
        if (this._current === 1) {
            pre = this._max;
        }
        else {
            pre = that._current - 1;
        }
        return pre;
    }
    //向左滚动
    Magic.prototype.left = function(next,current){
        var that = this;
        that.$imgList.find("li").eq(next-1).addClass(that.NEXT_CSS);
        that.$imgList.animate({"marginLeft":-(that._width)+"px"},500,function(){
            that.$imgs.eq(current-1).removeClass(that.NOW_CSS);
            that.$imgs.eq(next-1).removeClass(that.NEXT_CSS).addClass(that.NOW_CSS);
            that.$imgList.css({marginLeft:0});
        });
    }
    //向右滚动
    Magic.prototype.right = function(pre,current){
        var that = this;
        that.$imgList.find("li").eq(pre-1).addClass(that.PRE_CSS);
        that.$imgList.animate({"marginLeft":(that._width)+"px"},500,function(){
            that.$imgs.eq(current-1).removeClass(that.NOW_CSS)
            that.$imgs.eq(pre-1).removeClass(that.PRE_CSS).addClass(that.NOW_CSS);
            that.$imgList.css({marginLeft:0});
        });
    }
    //上一张图片
    Magic.prototype.preImg = function(){
        var that = this;
        that.$pre.click(function(){
            if (that.$imgList.queue().length > 0) return; //如果动画队列中有函数则直接返回
            var pre = that.pre();
            that.$nav.eq(pre-1).addClass("active").siblings().removeClass("active");
            that.right(pre,that._current);
            that._current=pre;
        });
    }
    //下一张图片
    Magic.prototype.nextImg = function(){
        var that = this;
        that.$next.click(function(){
            if (that.$imgList.queue().length > 0) return; //如果动画队列中有函数则直接返回
            var next = that.next();
            that.$nav.eq(next-1).addClass("active").siblings().removeClass("active");
            that.left(next,that._current);
            that._current=next;
        });
    }

    //显示对应标题
    Magic.prototype.showTitle = function(index){
        var that = this;
        var text = that.$imgs.eq(index).children("a").attr("title"),
            link = that.$imgs.eq(index).children("a").attr("href");
        var $head = that.obj.find("h3>a");
        if($head){$head.text(text);$head.attr("href",link);}
    }

    //点击索引显示相应图片
    Magic.prototype.indexImg = function(){
        var that = this;

        that.$nav.click(function(){
            if (that.$imgList.queue().length > 0) return; //如果动画队列中有函数则直接返回
            var vecIndex = $(this).index()+1;
            var dir = vecIndex - that._current;

            if(dir>0){
                var next = vecIndex;
                $(this).addClass("active").siblings().removeClass("active");
                that.left(next,that._current);
                that._current=next;
            }else if(dir<0){
                var pre = vecIndex;
                $(this).addClass("active").siblings().removeClass("active");
                that.right(pre,that._current);
                that._current=pre;
            }

            //显示对应标题
            that.showTitle(vecIndex-1);
        });
    }
    //图片自动切换
    Magic.prototype.magic = function(){
        var that = this;
        var magicTimer;//时间戳

        that.obj.hover(function(){
            clearInterval(magicTimer);
        },function(){
            magicTimer = setInterval(function(){
                var next = that.next();
                that.$nav.eq(next-1).addClass("active").siblings().removeClass("active");
                that.left(next,that._current);
                that._current=next;
                var index = next-1;

                //显示对应标题
                that.showTitle(index);
            },3000);
        }).trigger("mouseout");
    }
});